package at.fhj.itm10.ticketmaster.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import at.fhj.itm10.ticketmaster.dao.CustomerDAO;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.impl.DAOFactoryImpl;
import at.fhj.itm10.ticketmaster.models.Customer;

@RequestScoped
@ManagedBean(name = "activationBean")
public class ActivationBean {
	@ManagedProperty(value = "#{param.hash}")
	private String hash;
	@ManagedProperty(value = "#{param.email}")
	private String email;

	private boolean valid = false;
	private CustomerDAO dao;
	private Customer customer;
	private List<Customer> customers = new ArrayList<Customer>();

	public ActivationBean() {
		this.init();
	}

	@PostConstruct
	public void init() {
		FacesContext fc = FacesContext.getCurrentInstance();
		this.email = getParamEmail(fc);
		this.hash = getParamHash(fc);

		System.out.println(email);
		System.out.println(hash);
		try {
			DAOFactoryImpl daoFactoryImpl;
			daoFactoryImpl = new DAOFactoryImpl();
			setDao(daoFactoryImpl.getCustomerDAO());
		} catch (DAOException e) {
			e.printStackTrace();
		}
		valid = check(hash);

	}

	private boolean check(String hash2) {

		try {
			this.customers = this.dao.findByEMail(this.email);
			if (hash.equals(this.customers.get(0).getActivationKey())) {
				
				customer = this.customers.get(0);
				customer.setActivated(true);
				
				dao.update(customer);
				System.out.println(customers.get(0).isActivated());

				valid = true;
				return true;
			}

		} catch (DAOException e) {
			e.printStackTrace();
			System.out.println("Error Check method");
		}
		return false;
	}

	public String getParamEmail(FacesContext fc) {
		// String data = (String)
		// FacesContext.getCurrentInstance().getELContext().getELResolver()
		// .getValue(FacesContext.getCurrentInstance().getELContext(),
		// "#{param.email}", Object.class);

		// System.out.println("schiarches kind");
		Map<String, String> param = fc.getExternalContext()
				.getRequestParameterMap();
		System.out.println("Email=" + email);
		return param.get("email");

		// for (Map.Entry<String, String> entry : param.entrySet()) {
		// System.out.println("request " + entry.getKey() + " = " +
		// entry.getValue());
		// }
		//
		// return "dodl";

	}

	public String getParamHash(FacesContext fc) {
		Map<String, String> param = fc.getExternalContext()
				.getRequestParameterMap();
		System.out.println("hash=" + hash);
		return param.get("hash");
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public CustomerDAO getDao() {
		return dao;
	}

	public void setDao(CustomerDAO dao) {
		this.dao = dao;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
